########################################################################
#  Wiizard - A Wii games manager
#  Copyright (C) 2023  CYBERDEViL
#
#  This file is part of Wiizard.
#
#  Wiizard is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Wiizard is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################


from wiizard.const import (
  IMG_FRONT_COVER,
  IMG_FRONT_3D_COVER,
  IMG_DISC,
  IMG_FULL
)


class ScraperError(Exception):
  pass


class ScraperNotFoundError(ScraperError):
  pass


class ImgRequestProperties:
  def __init__(self, flags=IMG_FRONT_COVER, languages=None):
    """ImgRequestProperties - Describes the images to be downloaded.

     - When 'languages' is None it means first available language.
     - When multiple 'languages' are given it means that first language that
       is available for a requested image will be returned.
    """
    self.__flags = flags
    self.__langs = languages

  @property
  def languages(self):
    return self.__langs

  @property
  def flags(self):
    return self.__flags

  @property
  def front(self):
    return (self.flags & IMG_FRONT_COVER)

  @property
  def front3d(self):
    return (self.flags & IMG_FRONT_3D_COVER)

  @property
  def full(self):
    return (self.flags & IMG_FULL)

  @property
  def disc(self):
    return (self.flags & IMG_DISC)


class GameImageLink:
  def __init__(self, url, imgType, language):
    self.__url     = url
    self.__imgType = imgType
    self.__lang    = language

  @property
  def url(self):
    return self.__url

  @property
  def type(self):
    return self.__imgType

  @property
  def language(self):
    return self.__lang


class ScraperBase:
  def __init__(self, requestHandler):
    self.__requestHandler = requestHandler

  @staticmethod
  def selectImages(images, requestedProperties):
    # No language(s) specified, select first language that has requested images.
    if requestedProperties.languages is None:
      doneImgTypes = 0
      for img in images:
        # Already found this image type
        if (img.type & doneImgTypes):
          continue
        # New image
        if (img.type & requestedProperties.flags):
          yield img
          doneImgTypes |= img.type
      return

    # Select images by language and fallback languages
    doneImgTypes = 0
    for lang in requestedProperties.languages:
      for img in images:
        # Already satisfied
        if (img.type & doneImgTypes):
          continue

        # Not current language
        if (img.language != lang and lang != "*"):
          continue

        # New image
        if (img.type & requestedProperties.flags):
          yield img
          doneImgTypes |= img.type

  @property
  def request(self):
    return self.__requestHandler

  """ Scrapers should re-implement below!
  """

  def getGameImageLinks(self, gameId6Str):
    # Should return a list with 'GameImageLink' objects
    raise ScraperError("ScraperBase::getGameImageLinks() should be re-implemented!")
